 package manager; 
 
import entity.PlainTextMessage; 
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; 
import javax.jms.Connection; 
import javax.jms.ConnectionFactory; 
import javax.jms.Destination; 
import javax.jms.JMSException; 
import javax.jms.MessageProducer; 
import javax.jms.Session; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import session.stateless.MessageMeSessionLocal; 
 
 
public class MessageMeManager implements Serializable
{ 
    private MessageMeSessionLocal messageMeSessionLocal; 
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     
    public MessageMeManager() {} 
     
    public MessageMeManager(MessageMeSessionLocal messageMeSessionLocal) 
    { 
        this.messageMeSessionLocal = messageMeSessionLocal;         
    } 
     
     
     
    public List<PlainTextMessage> getAllMessages(String userName) 
    {                 
        return messageMeSessionLocal.getAllPlainTextMessages(userName); 
    } 
     
     
     
    public void messageMe(String message, String sender, String receiver, String title) 
    {     
        try 
        { 
            Context c = new InitialContext(); 
            ConnectionFactory cf = (ConnectionFactory) c.lookup("jms/queueMessageMeFactory"); 
            Connection conn = null; 
            Session s = null; 
 
            try  
            {             
                conn = cf.createConnection(); 
                s = conn.createSession(false, s.AUTO_ACKNOWLEDGE); 
                Destination destination = (Destination) c.lookup("jms/queueMessageMe"); 
                MessageProducer mp = s.createProducer(destination); 
 
                Date date = new Date();
                message = sender + "|" + receiver + "|" + dateFormat.format(date) + "|" + title + "|" + message;
                mp.send(s.createTextMessage(message)); 
            } 
            catch(JMSException jmsEx) 
            { 
                jmsEx.printStackTrace(); 
            } 
            finally  
            { 
                if (s != null) s.close();             
                if (conn != null) conn.close(); 
            } 
        } 
        catch(NamingException namingEx) 
        { 
            namingEx.printStackTrace(); 
        } 
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
    }    
    
    public void deleteMessage(Long id)
    {
       messageMeSessionLocal.deleteMessage(id);
    }
}     
